import { request } from '../request';

/** 获取社区可用游戏列表（侧边栏） */
export function fetchCommunityGameList() {
  return request<Array<{ id: number; game_name: string }>>({
    url: '/api/community/game-list.do',
    method: 'get'
  });
}

/** 获取指定游戏的社区帖子列表 */
export function fetchCommunityPostList(gameId: number) {
  return request<
    Array<{
      id: number;
      user_id: number;
      title: string;
      content: string;
      create_time: string;
    }>
  >({
    url: '/api/community/post-list.do',
    method: 'get',
    params: { game_id: gameId }
  });
}

/** 获取指定游戏的所有帖子详情（含内容、视频、图片、发布者用户名） */
export function fetchCommunityPostDetailList(gameId: number) {
  return request<
    Array<{
      id: number;
      game_id: number;
      user_id: number;
      username: string;
      title: string;
      content: string;
      video_url: string | null;
      images: string | null;
      create_time: string;
    }>
  >({
    url: '/api/community/post-detail-list.do',
    method: 'get',
    params: { game_id: gameId }
  });
}

/**
 * 新增社区帖子
 *
 * @param params 包含 game_id, user_id, title, content, flag, deleted, create_time, update_time
 * @returns {Promise<{ code: number; msg: string; data: any }>} 后端返回结构
 */
export function fetchCommunityPostCreate(params: {
  game_id: number;
  user_id: number;
  title: string;
  content: string;
  flag: number;
  deleted: number;
  create_time: string;
  update_time: string;
}) {
  return request<{ code: number; msg: string; data: any }>({
    url: '/api/community/post-create.do',
    method: 'post',
    headers: { 'Content-Type': 'application/json' },
    data: params
  });
}
